package com.wnxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wnxy.dto.StatusCode;
import com.wnxy.exception.UpdateException;
import com.wnxy.model.RoleNav;
import com.wnxy.mapper.RoleNavMapper;
import com.wnxy.service.RoleNavService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.util.CommonsUtil;
import com.wnxy.vo.RoleNavVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alice
 * @since 2021-11-12
 */
@Service
public class RoleNavServiceImpl extends ServiceImpl<RoleNavMapper, RoleNav> implements RoleNavService {

    @Override
    public void change(RoleNavVo roleNavVo) throws IllegalAccessException {
        if (!CommonsUtil.hasAllFields(roleNavVo)) throw new UpdateException(StatusCode.UPERROR.getCode(), "输入信息有误");
        //移除原有关系
        remove(new UpdateWrapper<RoleNav>().eq("role_id",roleNavVo.getRoleId()));
        List<RoleNav> roleNavs = new ArrayList<>();
        for (Integer navId : roleNavVo.getNavIds()) {
            RoleNav roleNav = new RoleNav();
            roleNav.setRoleId(roleNavVo.getRoleId());
            roleNav.setNavId(navId);
            roleNavs.add(roleNav);
        }
        boolean b = saveBatch(roleNavs);
        if(!b) throw new UpdateException(StatusCode.UPERROR.getCode(), "修改失败");

    }
}
